import { baseRequestClient } from '#/api/request'
import { requestClient } from '#/api/request'


export async function getStationsApi() {
  return baseRequestClient.get('http://localhost:5320/api/gis/xzj-station')
}
//-------------------------------
// 查询转油站基本信息列表
// 查询转油站参数
interface StationItem {
  stationCode?: string;
  stationName?: string;
}
// 查询转油站返回值
interface ListStationResult {
  rows: any;
  total: number;
}
//添加转油站参数
export interface AddStationParams extends Record<string, any> {
  stationCode: string;
  stationName: string;
  pipelingId: number;
  remark: string;
  location: string;
}

//修改转油站参数
export interface EditStationParams extends Record<string, any> {
  stationCode: string;
  stationName: string;
  pipelingId: number;
  remark: string;
  location: string;
}
//查询转油站在地图显示
export const getTransferStationsBygis = async () => {
  const response = await requestClient.get('/manage/station/list');
  const data = response.rows;
  const mappedResult = data.map((row: any) => {
    const location = row.location;
    let locationFormatted: [number,number] | undefined = undefined;
  
    if (location && location.startsWith('POINT')) {
      const [longitude,latitude] = location.match(/-?\d+(\.\d+)?/g)?.map(Number) || [];
      locationFormatted = [longitude,latitude];
    }
    
    return {
      ...row,
      location: locationFormatted,
    };
  });
  return mappedResult;
};

// 查询转油站列表
export const listStation = async (params: {
  pageNum: number;
  pageSize: number;
  stationCode?: string;
  stationName?: string;
}) => {
  const response = await requestClient.get<ListStationResult>('/manage/station/list', { params });
  const data = response.rows; // 提取 rows 字段
  let filteredData: StationItem[] = data;

  const total = response.total;
  const result = filteredData;

  const mappedResult = result.map((row: any) => {
    const location = row.location;
    let locationFormatted: string | undefined = undefined;

    if (location && location.startsWith('POINT')) {
      const [longitude, latitude] = location.match(/-?\d+(\.\d+)?/g)?.map(Number) || [];
      locationFormatted = `经度: ${longitude}, 纬度: ${latitude}`;
    }
    return {
      ...row,
      status: row.status === 1 ? '正常' : '冻结',
      location: locationFormatted,
    };
  });


  return {
    items: mappedResult,
    total: total,
  };
};


// 查询转油站详细
export async function getStation(id: number) {
  return requestClient.get(`/manage/station/${id}`);
}

// 新增转油站
export async function addStation(data: AddStationParams) {
  await requestClient.post('/manage/station', data);
}

// 修改转油站
export async function updateStation(data: EditStationParams) {
  requestClient.put('/manage/station', data);
}

// 删除转油站
export async function delStation(id: number) {
  requestClient.delete(`/manage/station/${id}`);
}
